﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Tank
{
    public class Player
    {
        TankSprite tank;
        ushort numShotsFired;
        ushort numHits;
        ushort numKills;
        ushort numDeaths;
		int health;
		float accuracy;
        PlayerIndex playerIndex;
        Color playerColor;

        public Player(PlayerIndex index)
        {
            playerIndex = index;
            numShotsFired = 0;
            numHits = 0;
            numKills = 0;
            numDeaths = 0;
			health = 0;
            tank = null;
			accuracy = 0;

            AssignColor();
        }

        public void AssignColor()
        {
            switch (playerIndex)
            { 
                case PlayerIndex.One:
                    playerColor = Color.Yellow;
                    break;

                case PlayerIndex.Two:
                    playerColor = Color.Blue;
                    break;

                case PlayerIndex.Three:
                    playerColor = Color.Green;
                    break;

                case PlayerIndex.Four:
                    playerColor = Color.Red;
                    break;
            }
        }

        public void Update(float deltaTime)
        {
            if (tank != null)
            {
				//JAB BUG - Removing this fixed a bug where tank can go through objects. (Multiple updates)
				// The tank is also being updated in the sprite list of the world.
                //tank.Update(deltaTime); 

                numShotsFired = tank.NumberShotsFired;
                numHits = tank.NumberHits;
                numDeaths = tank.NumberDeaths;
				health = tank.Health;
                numKills = tank.NumberKills;
				//accuracy = numShotsFired > 0? ((numHits / numShotsFired) * 100):0;
                //if (numShotsFired > 0)
                //{
                //     accuracy = (numHits / numShotsFired) * 100;
                //}
                //else
                //{
                //    accuracy = 0;
                //}
            }
        }

        public void CalcAccuracy()
        {
            if (numShotsFired > 0)
            {
                accuracy = ((float)numHits / (float)numShotsFired) * 100;
            }
            else
            {
                accuracy = 0;
            }
        }

        public void Draw(SpriteBatch sb)
        {
            if (tank != null)
            {
                tank.Draw(sb);
            }
        }

        public TankSprite Tank
        {
            get { return tank; }
            set { tank = value; }
        }

        // Returns player's accuracy as a percentage
        public float Accuracy
        {
			get { return accuracy; }
        }

        public ushort NumKills
        {
            get { return numKills; }
        }

        public ushort NumDeaths
        {
            get { return numDeaths; }
        }

        public void SetTank(TankSprite tankSprt)
        {
            tank = tankSprt;
        }

        public PlayerIndex PlayerIndexID
        {
            get { return playerIndex; }
        }

        public Color PlayerColor
        {
            get { return playerColor; }
        }

		public int Health
		{ 
			get { return health; }
		}


    }
}
